<template>
    <div>
        <div class="payBox flex">
            <p>支付方式</p>
            <div class="RadioBox">
                <p><el-checkbox v-model="checked">余额支付</el-checkbox><span>账户余额￥{{$store.state.userInfo.balance}}<i v-if='!isdisabled'>（当前余额不足）</i></span></p>
                <p><el-radio v-model="radio" :disabled='isdisabled'  label="3">支付宝</el-radio></p>
                <p><el-radio v-model="radio"  :disabled='isdisabled' label="2">微信支付</el-radio></p>
            </div>
        </div>
        <div class="btnBox flex flex-align-center flex-pack-center" @click="postCreateOrder"><p>确认套餐，去支付</p></div>
    </div>
</template>
<script>
import https from '@/public/https'
import {format} from '@/public/tools';
export default {
    data () {
        return {
            checked: true,
            radio: '3',
            isdisabled:false,

        }
    },
    props:{
        orderInfo:{
            type:Object
        }
    },
    created() {
        this.changeRouter();
    },
    watch:{
       orderInfo(){
           this.changeRouter();
       }
    },
    methods: {
      changeRouter(){
          if(Number(this.orderInfo.money) <= Number(this.$store.state.userInfo.balance)){
                this.isdisabled = true;
           }else{
                this.isdisabled = false;
           }
      },
      postCreateOrder(){
           let uid = this.$store.state.userInfo.id;
           let package_id = this.orderInfo.package_id;
           let package_title = this.orderInfo.package_title;
           let money =  this.orderInfo.money;
           let pay_balance = ''; //差额
           let pay_type = ''; //支付方式
           if(this.isdisabled){
               pay_balance = money;
               pay_type = 1;
           }else{
               pay_balance = this.$store.state.userInfo.balance;
               pay_type = this.radio;
           }
           https.post('recharge/postCreateOrder',{
               uid:uid,
               package_id:package_id,
               package_title:package_title,
               money:money,
               pay_balance:pay_balance,
               pay_type:pay_type
           })
           .then(data => {
               if(data.status == 1){
                   if(this.isdisabled){
                    //    余额充足  
                    https.get('recharge/orderPay',{order_id:data.data.id})
                    .then(data => {
                        if(data.status == 1){
                             this.$message({
                                message: data.message,
                                type: 'success'
                            });
                            // 更新详情
                            this.postObtainUserInfoByUid();
                            this.$emit('succ',data.data);//跳转成功页面
                        }else{
                            this.$message.error(data.message);
                        }
                    });
                   }else{
                    // 余额不足 走第三方支付;如果用户做升级操作，提示用户折算信息
                    let winUrl = window.location.href.split('#')[1];
                    let id = data.data.id;
                    if(winUrl == '/AgentRecharge' 
                        && this.$store.state.userInfo.member_type_int == 0 
                        && this.$store.state.userInfo.use_end_time != '1970/01/01 08:00:00'){
                            this.$confirm('选择升级，账户剩余软件使用时间将按时间比例折算处理，是否继续充值？', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                            }).then(() => {
                                this.$router.push({path: `/webview/${id}`});
                            }).catch(() => {      
                            });
                        }else{
                           this.$router.push({path: `/webview/${id}`});
                        }
                   }
               }else{
                    this.$message.error(data.message);
               }
           })
      },
      postObtainUserInfoByUid(){
          this.https('User/postObtainUserInfoByUid',{},'post')
          .then(data => {
                data.use_end_time = format(data.use_end_time,1);
                this.$store.commit('setUserInfo',data);
          })
      }
    }
}
</script>
<style lang="less" scoped>
@import url(../../../assets/css/usercenter/paybtnbox.less);
</style>


